#include <gtk/gtk.h>
#include "infodlg.h"
#include "misc.h"
#include "char_conv.h"

const gchar *blood_types[] = {
  "其它", "A", "B", "O", "AB",
};

const gchar *country_names[] = {
  "中国", "中国香港","中国澳门","中国台湾",
  "新加坡","马来西亚","美国",
};

const gchar *province_names[] = {
  "北京","天津","上海","重庆","香港",
  "河北","山西","内蒙古","辽宁","吉林",
  "黑龙江","江西","浙江","江苏","安徽",
  "福建","山东","河南","湖北","湖南",
  "广东","广西","海南","四川","贵州",
  "云南","西藏","陕西","甘肃","宁夏",
  "青海","新疆","台湾","澳门",
};

const gchar *zodiac_names[] = {
  "-", "鼠", "牛", "虎", "兔",
  "龙", "蛇", "马", "羊", "猴",
  "鸡", "狗", "猪",
};

const gchar *horoscope_names[] = {
  "-","水瓶座","双鱼座","牡羊座","金牛座",
  "双子座","巨蟹座","狮子座","处女座","天秤座",
  "天蟹座","射手座","魔羯座",
};

const gchar *occupation_names[] = {
  "全职","兼职","制造业","商业","失业中",
  "学生","工程师","政府部门","教育业","服务行业",
  "老板","计算机业","退休","金融业","销售/广告/市场",
};

/*****************************************************************************/
void no_edit(GtkWidget *w) {
  gtk_editable_set_editable(GTK_EDITABLE(w), FALSE);
}

/*****************************************************************************/
void qq_set_entry(GtkWidget *entry, gchar *text) {
	gchar *text_locale = qq_to_locale
		(text, strlen(text), QQ_DEFAULT_LOCALE);
	gtk_entry_set_text(GTK_ENTRY(entry), text_locale);
	g_free(text_locale);
}

/*****************************************************************************/
gchar *qq_get_entry(GtkWidget *entry){
	return locale_to_qq
		(gtk_entry_get_text(GTK_ENTRY(entry)), 
		 strlen(gtk_entry_get_text(GTK_ENTRY(entry))), QQ_DEFAULT_LOCALE);
}

/*****************************************************************************/
void qq_set_text(GtkWidget *entry, gchar *text) {
	gchar *text_locale = qq_to_locale
		(text, strlen(text), QQ_DEFAULT_LOCALE);
	gtk_text_buffer_set_text
		(gtk_text_view_get_buffer(GTK_TEXT_VIEW(entry)), text_locale, -1);
	g_free(text_locale);
}

/*****************************************************************************/
gchar *qq_get_text(GtkWidget *entry) {
	GtkTextIter start, end;
	gchar *str, *ret;

	gtk_text_buffer_get_bounds
		(gtk_text_view_get_buffer(GTK_TEXT_VIEW(entry)),&start,&end);
	str = gtk_text_buffer_get_text(gtk_text_view_get_buffer
		(GTK_TEXT_VIEW(entry)),&start,&end,FALSE);
	ret = locale_to_qq(str,-1,QQ_DEFAULT_LOCALE);
	g_free(str);

	return ret;
}

/*****************************************************************************/
void qq_set_image(GtkWidget *entry, int index) {
	GdkPixbuf *pixbuf;
	pixbuf = get_face_gdkpixbuf(index);
	gtk_image_set_from_pixbuf(GTK_IMAGE(entry), pixbuf);
	g_object_unref(pixbuf);
  g_object_set_data(G_OBJECT(entry),"user_data",GINT_TO_POINTER(index));
}

/*****************************************************************************/
GList *get_list_by_array(gchar **array, int size) {
	GList *cbitems = NULL;
	int i;
	for (i=0; i<size; i++)
    cbitems = g_list_append (cbitems, utf8(array[i]));
	return cbitems;
}

/*****************************************************************************/
gchar *get_name_by_index_str
(gchar **array, gchar *index_str, int min, int max){
  int index = atoi(index_str);
  if ((index < min)||(index > max)) index = 0;
  return utf8(array[index]);
}

/*****************************************************************************/
void get_index_str_by_name 
(gchar **array, gchar *name, gchar* index_str, int min, int max) {
  int index=0;
  gchar *name_utf8 = g_locale_to_utf8(name, -1, NULL, NULL, NULL);
  for (index=min; index<=max; index++)
    if (! strcmp(array[index], name_utf8)) break;
  
  if (index > max) index = 0;// meaning no match
  sprintf(index_str, "%d", index);
}

/*****************************************************************************/
void qq_set_open_contact_radio
(struct _updating_contact_info *updateinfo, gchar *is_open_contact) {
  int open = atoi(is_open_contact);
	GtkWidget **radio = updateinfo->open_contact_radio;
  if (open == QQ_CONTACT_CLOSE) 
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio[2]), TRUE);
  else if (open == QQ_CONTACT_ONLY_FRIENDS) 
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio[1]), TRUE);
  else  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio[0]), TRUE);    
}

/*****************************************************************************/
void qq_set_auth_type_radio
(struct _updating_contact_info *updateinfo, gchar *auth_type_str) {
  int auth_type = atoi(auth_type_str);
	GtkWidget **radio = updateinfo->auth_radio;
  if (auth_type == QQ_AUTH_NO_AUTH) 
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio[0]), TRUE);
  else if (auth_type == QQ_AUTH_NEED_AUTH) 
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio[1]), TRUE);
  else gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio[2]), TRUE); 
}

/*****************************************************************************/
static void info_window_destroy 
(GtkWidget * widget, gpointer data) {
	GaimConnection *gc = (GaimConnection *)data;
	struct qq_data *qd = gc->proto_data;
	GList *list= qd->updateinfo;
	struct _updating_contact_info *updateinfo;

	while (list) {
		updateinfo = (struct _updating_contact_info *)(list->data);
		if (updateinfo->window == widget) {
		  if (updateinfo->old_info) g_free(updateinfo->old_info);
			g_free(updateinfo);
			qd->updateinfo = 
				g_list_remove(qd->updateinfo,updateinfo);
			break;
		}
		else list = list->next;
	}
}

/*****************************************************************************/
void info_window_deleteevent 
(GtkWidget * widget, GdkEvent * event, gpointer data) {
	gtk_widget_destroy (widget);
}

/*****************************************************************************/
void info_window_close(GtkWidget * widget, gpointer data) {	
	gtk_widget_destroy (GTK_WIDGET(data));
}
	
/*****************************************************************************/
void info_window_refresh(GtkWidget * widget, gpointer data) {

	GaimConnection *gc = (GaimConnection *)data;
	struct _updating_contact_info *updateinfo	= 
		(struct _updating_contact_info *)
		g_object_get_data(G_OBJECT(widget),"user_data");

	qq_send_packet_get_info(gc, updateinfo->uid, TRUE);

	gtk_widget_set_sensitive(updateinfo->refresh_button,FALSE);
}

/*****************************************************************************/
static void info_window_change(GtkWidget * widget, gpointer data) {
	struct _updating_contact_info *updateinfo	= 
		(struct _updating_contact_info *)
		g_object_get_data(G_OBJECT(widget),"user_data");
	GaimConnection *gc = (GaimConnection *)data;
	GaimAccount *a = gc->account;
	struct qq_data *qd = gc->proto_data;
	struct contact_info *info = updateinfo->old_info;
	
	gboolean is_modify_passwd = FALSE;
	gchar *passwd[3]; passwd[0]=passwd[1]=passwd[2]=NULL;
	uint8 icon, auth_type, open_contact;
	gchar icon_index_str[4], auth_type_str[2], open_contact_str[2];
	gchar blood_index_str[2], zodiac_index_str[3], horoscope_index_str[3];

	if (gtk_toggle_button_get_active
		(GTK_TOGGLE_BUTTON(updateinfo->is_modify_passwd)))
		is_modify_passwd = TRUE;
		
	if (is_modify_passwd) {
		passwd[0] = qq_get_entry(updateinfo->old_password);
		passwd[1] = qq_get_entry(updateinfo->password[0]);
		passwd[2] = qq_get_entry(updateinfo->password[1]);
		if (strcmp(a->password, passwd[0])) {
			gaim_notify_error(gc, _("Error"), utf8("旧密码错误!"), NULL);
			g_free(passwd[0]); g_free(passwd[1]); g_free(passwd[2]);
			return;
		}
		if (passwd[1][0]=='\0') {
			gaim_notify_error(gc, _("Error"), utf8("密码不能为空!"), NULL);
			g_free(passwd[0]); g_free(passwd[1]); g_free(passwd[2]);
			return;
		}
		if (strcmp(passwd[1], passwd[2])) {
			gaim_notify_error(gc, _("Error"),utf8("新密码两次输入的不同!"), NULL);
			g_free(passwd[0]); g_free(passwd[1]); g_free(passwd[2]);
			return;
		}
		if (strlen(passwd[1]) > 16) {
			gaim_notify_error(gc, _("Error"),utf8("密码不能多于16个字符!"), NULL);
			g_free(passwd[0]); g_free(passwd[1]); g_free(passwd[2]);
			return;
		}
	}
	
	icon = (int)g_object_get_data(G_OBJECT(updateinfo->face),"user_data");
	sprintf(icon_index_str, "%d", icon);
	info->face = icon_index_str;

	info->nick = qq_get_entry(updateinfo->nick);
	info->age = qq_get_entry(updateinfo->age);
	info->gender = qq_get_entry(updateinfo->gender);
	info->country = qq_get_entry(updateinfo->country);
	info->province = qq_get_entry(updateinfo->province);
	info->city = qq_get_entry(updateinfo->city);
	info->email = qq_get_entry(updateinfo->email);
	info->address = qq_get_entry(updateinfo->address);
	info->zipcode = qq_get_entry(updateinfo->zipcode);
	info->tel = qq_get_entry(updateinfo->tel);
	info->name = qq_get_entry(updateinfo->name);
	info->college = qq_get_entry(updateinfo->college);
	info->occupation = qq_get_entry(updateinfo->occupation);
	info->homepage = qq_get_entry(updateinfo->homepage);

	info->intro = qq_get_text(updateinfo->intro);
 
  get_index_str_by_name((gchar **)blood_types, 
      (gchar *)gtk_entry_get_text(GTK_ENTRY(updateinfo->blood)), 
      (gchar *)blood_index_str, 0, 4);
  get_index_str_by_name((gchar **)zodiac_names, 
      (gchar *)gtk_entry_get_text(GTK_ENTRY(updateinfo->zodiac)), 
      (gchar *)zodiac_index_str, 0, 12);
  get_index_str_by_name((gchar **)horoscope_names, 
      (gchar *)gtk_entry_get_text(GTK_ENTRY(updateinfo->horoscope)),
      (gchar *)horoscope_index_str, 0, 12);
      
	info->blood = blood_index_str;
  info->zodiac = zodiac_index_str;
  info->horoscope = horoscope_index_str;
  	
	if (gtk_toggle_button_get_active
    (GTK_TOGGLE_BUTTON(updateinfo->auth_radio[0])))
		auth_type = QQ_AUTH_NO_AUTH;
	else if (gtk_toggle_button_get_active
		(GTK_TOGGLE_BUTTON(updateinfo->auth_radio[1])))
		auth_type = QQ_AUTH_NEED_AUTH;
	else auth_type = QQ_AUTH_NO_ADD;
	sprintf(auth_type_str, "%d", auth_type);
	info->auth_type = auth_type_str;
	
	if (gtk_toggle_button_get_active
    (GTK_TOGGLE_BUTTON(updateinfo->open_contact_radio[0])))
		open_contact = QQ_CONTACT_OPEN;
	else if (gtk_toggle_button_get_active
		(GTK_TOGGLE_BUTTON(updateinfo->open_contact_radio[1])))
		open_contact = QQ_CONTACT_ONLY_FRIENDS;
	else open_contact = QQ_CONTACT_CLOSE;
	sprintf(open_contact_str, "%d", open_contact);
	info->is_open_contact = open_contact_str;
	
	qq_send_packet_modify_info(gc, info, passwd[1]);
	
	qd->my_icon = icon; // fresh face and nick
	gaim_account_set_alias
		(gc->account,gtk_entry_get_text(GTK_ENTRY(updateinfo->nick)));
	
	g_free(info->nick);
	g_free(info->gender);
	g_free(info->country);
	g_free(info->province);
	g_free(info->city);
	g_free(info->email);
	g_free(info->address);
	g_free(info->zipcode);
	g_free(info->tel);
	g_free(info->name);
	g_free(info->college);
	g_free(info->occupation);
	g_free(info->homepage);
	g_free(info->intro);
	
	if (passwd[1]) {g_free(passwd[0]); g_free(passwd[1]); g_free(passwd[2]);}

	gtk_widget_destroy(updateinfo->window); //close window
}

/*****************************************************************************/
void set_face(GtkWidget *w, struct _updating_contact_info *updateinfo) {
	int i = (int)g_object_get_data(G_OBJECT(w),"user_data");
	qq_set_image(updateinfo->face, i*3);
}

/*****************************************************************************/
static void info_window_change_face 
(GtkWidget * widget, GdkEvent * event, 
 struct _updating_contact_info *updateinfo) {
	GtkWidget *dialog;
	GtkWidget *vbox, *smiley_box=NULL;
	GtkWidget *image;
	GdkPixbuf *pixbuf;
	GtkWidget *button;
	int i;

	dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
	gtk_window_set_transient_for
		(GTK_WINDOW(dialog),GTK_WINDOW(updateinfo->window));
	gtk_window_set_modal(GTK_WINDOW(dialog),TRUE);
	g_signal_connect (G_OBJECT (dialog), "delete_event",
			    G_CALLBACK(info_window_deleteevent), NULL);

	gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
	gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
	
	vbox = gtk_vbox_new(TRUE, 5);

	for (i=0; i<85; i++) {
		if (! (i % 8)) {
			smiley_box = gtk_toolbar_new();
			gtk_box_pack_start(GTK_BOX(vbox), smiley_box, TRUE, TRUE, 0);
		}

		pixbuf = get_face_gdkpixbuf(i*3);		
		image = gtk_image_new_from_pixbuf(pixbuf);
		g_object_unref(pixbuf);
		button = gtk_toolbar_append_item
			(GTK_TOOLBAR(smiley_box), NULL, NULL, NULL,
					image, G_CALLBACK(set_face), updateinfo);		
		g_object_set_data(G_OBJECT(button),"user_data",GINT_TO_POINTER(i));
		g_signal_connect (G_OBJECT (button), "clicked",
			    G_CALLBACK(info_window_close), dialog);
		gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
	}
	
	gtk_container_add(GTK_CONTAINER(dialog), vbox);
	gtk_container_set_border_width(GTK_CONTAINER(dialog), 5);
	gtk_window_set_title(GTK_WINDOW(dialog), _("Choose my head icon"));
	gtk_widget_show_all(dialog);
}

/*****************************************************************************/
GtkWidget * createpage1 
(int is_myself, struct contact_info *info, 
 GaimConnection *gc,struct _updating_contact_info *updateinfo) {
	GtkWidget *hbox;
	GtkWidget *pixmap;
	GdkPixbuf *pixbuf;
	GList *cbitems = NULL;
	GtkWidget *frame;
	GtkWidget *label;
	GtkWidget *table;
	GtkWidget *entry;
	GtkWidget *combo;
	GtkWidget *alignment;
		
	hbox = gtk_hbox_new(FALSE,0);
	frame = gtk_frame_new(utf8(""));
	gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
	gtk_box_pack_start(GTK_BOX(hbox),frame,TRUE,TRUE,0);
	table = gtk_table_new(7,3,FALSE);
	gtk_table_set_row_spacings(GTK_TABLE(table), 2);
	gtk_table_set_col_spacings(GTK_TABLE(table), 5);
	gtk_container_set_border_width (GTK_CONTAINER (table), 5);
	gtk_container_add (GTK_CONTAINER (frame), table);
	pixbuf = get_face_gdkpixbuf(atoi(info->face));
	pixmap = gtk_image_new_from_pixbuf(pixbuf);
	updateinfo->face = pixmap;
	g_object_unref(pixbuf);
	alignment = gtk_alignment_new(1, 0, 0,0);
	gtk_widget_set_usize(alignment, 50,70);
	if (is_myself) {
		GtkWidget *event_box;
		g_object_set_data(G_OBJECT(updateinfo->face),
			"user_data",GINT_TO_POINTER((gint)atoi(info->face)));	
		event_box = gtk_event_box_new();
		g_signal_connect (G_OBJECT (event_box), "button_press_event",
			    G_CALLBACK(info_window_change_face), updateinfo);
		gtk_container_add(GTK_CONTAINER(event_box),pixmap);
		gtk_container_add(GTK_CONTAINER(alignment), event_box);
	} else 
    gtk_container_add(GTK_CONTAINER(alignment), pixmap);
    
  gtk_table_attach(GTK_TABLE(table),alignment,2,3,0,3,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("用户号码:"));
	gtk_misc_set_alignment (GTK_MISC (label), 1, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,0,1,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("用户昵称:"));
	gtk_misc_set_alignment (GTK_MISC (label), 1, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,1,2,GTK_FILL,0,0,0);
	entry = gtk_entry_new();
	updateinfo->uid_entry = entry;
	qq_set_entry(updateinfo->uid_entry, info->uid);
	gtk_widget_set_size_request (entry, 120, -1);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,0,1,GTK_FILL,0,0,0);
	entry = gtk_entry_new();
	updateinfo->nick = entry;
	qq_set_entry(updateinfo->nick, info->nick);
	gtk_widget_set_size_request (entry, 120, -1);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,1,2,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("年龄:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,2,3,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("性别:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,3,4,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("国家/地区:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,4,5,GTK_FILL,0,0,0);
	
	combo = gtk_combo_new();
	cbitems = get_list_by_array((gchar **)country_names, 7);
	gtk_combo_set_popdown_strings (GTK_COMBO (combo), cbitems);
	g_list_free(cbitems);
	cbitems = NULL;
	updateinfo->country = GTK_COMBO(combo)->entry;
	qq_set_entry(updateinfo->country, info->country);
	gtk_widget_set_size_request (combo, 150, -1);
	gtk_table_attach(GTK_TABLE(table),combo,1,2,4,5,GTK_FILL,0,0,0);
	if (! is_myself) gtk_widget_set_sensitive(combo, FALSE);
	
	entry = gtk_entry_new();
	updateinfo->age = entry;
	qq_set_entry(updateinfo->age, info->age);
	gtk_widget_set_size_request (entry, 40, -1);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,2,3,GTK_FILL,0,0,0);
	
	combo = gtk_combo_new();
	cbitems = g_list_append (cbitems, utf8("男"));
	cbitems = g_list_append (cbitems, utf8("女"));
	gtk_combo_set_popdown_strings (GTK_COMBO (combo), cbitems);
	g_list_free(cbitems);
	cbitems = NULL;
	updateinfo->gender = GTK_COMBO(combo)->entry;
	gtk_widget_set_size_request (combo, 60, -1);
	qq_set_entry(updateinfo->gender, info->gender);
	gtk_editable_set_editable(GTK_EDITABLE(GTK_COMBO(combo)->entry),FALSE);
	gtk_table_attach(GTK_TABLE(table),combo,1,2,3,4,GTK_FILL,0,0,0);
	if (! is_myself) gtk_widget_set_sensitive(combo, FALSE);
	
	label = gtk_label_new(utf8("省份:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,5,6,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("城市:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,6,7,GTK_FILL,0,0,0);
	
	combo = gtk_combo_new();
  cbitems = get_list_by_array((gchar **)province_names, 29);
	gtk_combo_set_popdown_strings (GTK_COMBO (combo), cbitems);
	g_list_free(cbitems);
	cbitems = NULL;
	updateinfo->province = GTK_COMBO(combo)->entry;
	qq_set_entry(updateinfo->province, info->province);
	gtk_widget_set_size_request (combo, 150, -1);
	gtk_table_attach(GTK_TABLE(table),combo,1,2,5,6,GTK_FILL,0,0,0);
	if (! is_myself) gtk_widget_set_sensitive(combo, FALSE);
	
	entry = gtk_entry_new();
	updateinfo->city = entry;
	gtk_widget_set_size_request (entry, 120, -1);
	qq_set_entry(updateinfo->city, info->city);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,6,7,GTK_FILL,0,0,0);
	
	return hbox;
}

/*****************************************************************************/
GtkWidget * createpage2 
(int is_myself,struct contact_info *info, 
 GaimConnection *gc,struct _updating_contact_info *updateinfo) {
	GtkWidget *hbox1;
	GtkWidget *entry;
	GtkWidget *label;
	GtkWidget *hbox;
	GtkWidget *radio;
	GtkWidget *table;
	GtkWidget *vbox1;
	GtkWidget *frame;
	
	hbox = gtk_hbox_new(FALSE,0);
	vbox1 = gtk_vbox_new(FALSE,0);
	gtk_box_pack_start(GTK_BOX(hbox),vbox1,TRUE,TRUE,0);
	frame = gtk_frame_new(utf8(""));
	gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
	gtk_box_pack_start(GTK_BOX(vbox1),frame,TRUE,TRUE,0);
	table = gtk_table_new(2,5,FALSE);
	gtk_table_set_row_spacings(GTK_TABLE(table), 2);
	gtk_table_set_col_spacings(GTK_TABLE(table), 5);
	gtk_container_set_border_width (GTK_CONTAINER (table), 5);
	gtk_container_add (GTK_CONTAINER (frame), table);
	label = gtk_label_new(utf8("电子邮件:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,0,1,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("联系地址:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,1,2,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("邮政编码:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,2,3,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("电话号码:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,3,4,GTK_FILL,0,0,0);
  label = gtk_label_new(utf8("个人主页:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0, .5);             
  gtk_table_attach(GTK_TABLE(table),label,0,1,4,5,GTK_FILL,0,0,0);
	
	
	entry = gtk_entry_new();
	updateinfo->email = entry;
	gtk_widget_set_size_request(entry, 200, -1);
	qq_set_entry(updateinfo->email, info->email);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,0,1,GTK_FILL,0,0,0);
	
	entry = gtk_entry_new();
	updateinfo->address = entry;
	qq_set_entry(updateinfo->address, info->address);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,1,2,GTK_FILL,0,0,0);
	
	entry = gtk_entry_new();
	updateinfo->zipcode = entry;
	qq_set_entry(updateinfo->zipcode, info->zipcode);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,2,3,GTK_FILL,0,0,0);
	
	entry = gtk_entry_new();
	updateinfo->tel = entry;
	qq_set_entry(updateinfo->tel, info->tel);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,3,4,GTK_FILL,0,0,0);

	entry = gtk_entry_new();
	updateinfo->homepage = entry;
	qq_set_entry(updateinfo->homepage, info->homepage);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,4,5,GTK_FILL,0,0,0);

	frame = gtk_frame_new(utf8("以上资料"));
	gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
	if (! is_myself) gtk_widget_set_sensitive(frame,FALSE);
	gtk_box_pack_start(GTK_BOX(vbox1),frame,FALSE,FALSE,0);
	
	hbox1 = gtk_hbox_new(FALSE,5);
	gtk_container_add (GTK_CONTAINER (frame), hbox1);
	radio = gtk_radio_button_new_with_label(NULL,utf8("完全公开"));
	updateinfo->open_contact_radio[0] = radio;
	gtk_box_pack_start(GTK_BOX(hbox1),radio,FALSE,FALSE,0);
	radio = gtk_radio_button_new_with_label_from_widget
		(GTK_RADIO_BUTTON(radio),utf8("仅好友可见"));
  updateinfo->open_contact_radio[1] = radio;
	gtk_box_pack_start(GTK_BOX(hbox1),radio,FALSE,FALSE,0);
	radio = gtk_radio_button_new_with_label_from_widget
		(GTK_RADIO_BUTTON(radio),utf8("完全保密"));
	updateinfo->open_contact_radio[2] = radio;
	gtk_box_pack_start(GTK_BOX(hbox1),radio,FALSE,FALSE,0);	

  qq_set_open_contact_radio(updateinfo, info->is_open_contact);
  
	return hbox;
}

/*****************************************************************************/
GtkWidget * createpage3 
(int is_myself, struct contact_info *info, 
 GaimConnection *gc,struct _updating_contact_info *updateinfo) {
	GtkWidget *text;
	GtkWidget *hbox;
	GtkWidget *frame;
	GList *cbitems = NULL;
	GtkWidget *combo;
	GtkWidget *table;
	GtkWidget *label;
	GtkWidget *entry;
	GtkWidget *scrolled_window;
	
	hbox = gtk_hbox_new(FALSE,0);
	frame = gtk_frame_new(utf8(""));
	gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
	gtk_box_pack_start(GTK_BOX(hbox),frame,TRUE,TRUE,0);
	table = gtk_table_new(4,4,FALSE);
  gtk_table_set_row_spacings(GTK_TABLE(table), 2);
  gtk_table_set_col_spacings(GTK_TABLE(table), 5);	
	gtk_container_set_border_width (GTK_CONTAINER (table), 5);
	gtk_container_add (GTK_CONTAINER (frame), table);
	label = gtk_label_new(utf8("真实姓名:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,0,1,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("　生肖:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,2,3,0,1,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("　血型:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,2,3,1,2,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("　星座:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,2,3,2,3,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("毕业院校:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,1,2,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("职业:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,2,3,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("个人说明:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
	gtk_table_attach(GTK_TABLE(table),label,0,1,3,4,GTK_FILL,0,0,0);

	entry = gtk_entry_new();
	updateinfo->name = entry;
	gtk_widget_set_size_request(entry, 120, -1);
	qq_set_entry(updateinfo->name, info->name);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,0,1,GTK_FILL,0,0,0);
	
	entry = gtk_entry_new();
	updateinfo->college = entry;
	gtk_widget_set_size_request(entry, 120, -1);
	qq_set_entry(updateinfo->college, info->college);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,1,2,GTK_FILL,0,0,0);
	
	combo = gtk_combo_new();
	cbitems = get_list_by_array((gchar **)zodiac_names, 13);
	gtk_combo_set_popdown_strings (GTK_COMBO (combo), cbitems);
	g_list_free(cbitems);
	cbitems = NULL;
	updateinfo->zodiac = GTK_COMBO(combo)->entry;
	// not use qq_set_entry, as the string is not in QQ locale
	gtk_entry_set_text(GTK_ENTRY(updateinfo->zodiac), 
	  get_name_by_index_str((gchar **)zodiac_names, info->zodiac, 1, 12));
	gtk_widget_set_size_request (combo, 70, -1);
	gtk_editable_set_editable(GTK_EDITABLE(GTK_COMBO(combo)->entry),FALSE);
	gtk_table_attach(GTK_TABLE(table),combo,3,4,0,1,GTK_FILL,0,0,0);
  if (! is_myself) gtk_widget_set_sensitive(combo, FALSE);
  
	combo = gtk_combo_new();
	cbitems = get_list_by_array((gchar **)blood_types, 5);
	gtk_combo_set_popdown_strings (GTK_COMBO (combo), cbitems);
	g_list_free(cbitems);
	cbitems = NULL;
	updateinfo->blood = GTK_COMBO(combo)->entry;
	gtk_entry_set_text(GTK_ENTRY(updateinfo->blood), 
    get_name_by_index_str((gchar **)blood_types, info->blood, 1, 4));
	gtk_widget_set_size_request (combo, 70, -1);
	gtk_editable_set_editable(GTK_EDITABLE(GTK_COMBO(combo)->entry),FALSE);
	gtk_table_attach(GTK_TABLE(table),combo,3,4,1,2,GTK_FILL,0,0,0);
  if (! is_myself) gtk_widget_set_sensitive(combo, FALSE);

	combo = gtk_combo_new();
	cbitems = get_list_by_array((gchar **)horoscope_names, 13);
	gtk_combo_set_popdown_strings (GTK_COMBO (combo), cbitems);
	g_list_free(cbitems);
	cbitems = NULL;
	updateinfo->horoscope = GTK_COMBO(combo)->entry;
	gtk_entry_set_text(GTK_ENTRY(updateinfo->horoscope),
    get_name_by_index_str((gchar **)horoscope_names, info->horoscope, 1, 12));
	gtk_widget_set_size_request (combo, 70, -1);
	gtk_editable_set_editable(GTK_EDITABLE(GTK_COMBO(combo)->entry),FALSE);
	gtk_table_attach(GTK_TABLE(table),combo,3,4,2,3,GTK_FILL,0,0,0);
	if (! is_myself) gtk_widget_set_sensitive(combo, FALSE);
	
	combo = gtk_combo_new();
	cbitems = get_list_by_array((gchar **)occupation_names, 15);
	gtk_combo_set_popdown_strings (GTK_COMBO (combo), cbitems);
	g_list_free(cbitems);
	cbitems = NULL;
	updateinfo->occupation = GTK_COMBO(combo)->entry;
	qq_set_entry(updateinfo->occupation, info->occupation);
	gtk_widget_set_size_request (combo, 120, -1);
	gtk_table_attach(GTK_TABLE(table),combo,1,2,2,3,GTK_FILL,0,0,0);
  if (! is_myself) gtk_widget_set_sensitive(combo, FALSE);
  
	text = gtk_text_view_new();
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
	updateinfo->intro = text;
	gtk_widget_set_size_request (text, -1, 90);
	qq_set_text(updateinfo->intro, info->intro);

	scrolled_window = gtk_scrolled_window_new(NULL,NULL);
	gtk_scrolled_window_set_policy 
		(GTK_SCROLLED_WINDOW (scrolled_window),
		GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
	gtk_container_add(GTK_CONTAINER(scrolled_window),text);
	gtk_scrolled_window_set_shadow_type 
		(GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_IN);
	gtk_text_view_set_left_margin (GTK_TEXT_VIEW (text), 2);
	gtk_text_view_set_right_margin (GTK_TEXT_VIEW (text), 2);
	
	gtk_table_attach(GTK_TABLE(table),scrolled_window,1,4,3,4,GTK_FILL,0,0,0);
	return hbox;
}

/*****************************************************************************/
GtkWidget * createpage4 
(int is_myself, struct contact_info *info, 
 GaimConnection *gc,struct _updating_contact_info *updateinfo) {
	GtkWidget *label;
	label = gtk_label_new(_("GAIM(QQ)-0.76cvs\n\nQQ plug-in for GAIM 0.76+ is develped by\nChen Peng <chenpeng@alumni.nus.edu.sg>"));
	return label;
}

/*****************************************************************************/
void change_passwd_checkbutton_callback
(GtkWidget *widget,struct _updating_contact_info *updateinfo)
{
	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) {
		gtk_widget_set_sensitive(updateinfo->old_password,TRUE);
		gtk_widget_set_sensitive(updateinfo->password[0],TRUE);
		gtk_widget_set_sensitive(updateinfo->password[1],TRUE);
	} else {
		gtk_widget_set_sensitive(updateinfo->old_password,FALSE);
		gtk_widget_set_sensitive(updateinfo->password[0],FALSE);
		gtk_widget_set_sensitive(updateinfo->password[1],FALSE);
	}
}

/*****************************************************************************/
GtkWidget * createpage5 
(int is_myself, struct contact_info *info, 
 GaimConnection *gc,struct _updating_contact_info *updateinfo) {
	GtkWidget *hbox;
	GtkWidget *frame;
	GtkWidget *vbox2;
	GtkWidget *entry;
	GtkWidget *vbox1;
	GtkWidget *table;
	GtkWidget *label;
	GtkWidget *check_button;
	GtkWidget *radio, *alignment;
	
  hbox = gtk_hbox_new(FALSE,0);
	vbox1 = gtk_vbox_new(FALSE,0);
	gtk_box_pack_start(GTK_BOX(hbox),vbox1,TRUE,TRUE,0);
	frame = gtk_frame_new(utf8("修改密码"));
	gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
	gtk_box_pack_start(GTK_BOX(vbox1),frame,FALSE,FALSE,0);
	table = gtk_table_new(3,3,FALSE);
  gtk_table_set_row_spacings(GTK_TABLE(table), 2);
  gtk_table_set_col_spacings(GTK_TABLE(table), 5);
	gtk_container_set_border_width (GTK_CONTAINER (table), 5);
	gtk_container_add (GTK_CONTAINER (frame), table);
	label = gtk_label_new(utf8("旧口令:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,0,1,GTK_FILL,0,0,0);
	check_button = gtk_check_button_new_with_label(utf8("我要修改"));
	updateinfo->is_modify_passwd = check_button;
	g_signal_connect (G_OBJECT (check_button), "toggled", 
		G_CALLBACK (change_passwd_checkbutton_callback), updateinfo);   			   
	gtk_table_attach(GTK_TABLE(table),check_button,2,3,0,1,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("新口令:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,1,2,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("(16字符以内)"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,2,3,1,2,GTK_FILL,0,0,0);
	label = gtk_label_new(utf8("口令确认:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0, .5);
	gtk_table_attach(GTK_TABLE(table),label,0,1,2,3,GTK_FILL,0,0,0);
	
	entry = gtk_entry_new();
	gtk_entry_set_visibility(GTK_ENTRY(entry),FALSE);
	gtk_widget_set_sensitive(entry,FALSE);
	updateinfo->old_password = entry;
	gtk_widget_set_size_request (entry, 160, -1);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,0,1,GTK_FILL,0,0,0);
	
	entry = gtk_entry_new();
	gtk_entry_set_visibility(GTK_ENTRY(entry),FALSE);
	gtk_widget_set_sensitive(entry,FALSE);
	updateinfo->password[0] = entry;
	gtk_widget_set_size_request (entry, 160, -1);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,1,2,GTK_FILL,0,0,0);
	
	entry = gtk_entry_new();
	gtk_entry_set_visibility(GTK_ENTRY(entry),FALSE);
	gtk_widget_set_sensitive(entry,FALSE);
	updateinfo->password[1] = entry;
	gtk_widget_set_size_request (entry, 160, -1);
	gtk_table_attach(GTK_TABLE(table),entry,1,2,2,3,GTK_FILL,0,0,0);

  alignment = gtk_alignment_new(0,0,0,0);
  gtk_widget_set_usize(alignment, -1, 20);
  gtk_box_pack_start(GTK_BOX(vbox1), alignment, FALSE, FALSE, 0);
  
	frame = gtk_frame_new(utf8("安全认证"));
	gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
	gtk_box_pack_start(GTK_BOX(vbox1),frame,FALSE,FALSE,0);
	
	vbox2 = gtk_vbox_new(FALSE,1);
	gtk_container_add (GTK_CONTAINER (frame), vbox2);
	
	radio = gtk_radio_button_new_with_label
		(NULL,_("允许任何人把我列为好友"));
	updateinfo->auth_radio[0] = radio;
	gtk_box_pack_start(GTK_BOX(vbox2),radio,FALSE,FALSE,0);	
	radio = gtk_radio_button_new_with_label_from_widget
		(GTK_RADIO_BUTTON(radio),utf8("需要身份验证才能把我列为好友"));
	updateinfo->auth_radio[1] = radio;
	gtk_box_pack_start(GTK_BOX(vbox2),radio,FALSE,FALSE,0);
	radio = gtk_radio_button_new_with_label_from_widget
		(GTK_RADIO_BUTTON(radio),utf8("不允许任何人把我列为好友"));
	updateinfo->auth_radio[2] = radio;
	gtk_box_pack_start(GTK_BOX(vbox2),radio,FALSE,FALSE,0);	

  qq_set_auth_type_radio(updateinfo, info->auth_type);
  
	return hbox;
}

/*****************************************************************************/
void qq_show_contact_info_dialog
(struct contact_info *info, GaimConnection *gc,
 struct _updating_contact_info *updateinfo) {
	GaimAccount *a = gc->account;
	int is_myself;
	GtkWidget *label;	
	GtkWidget *window;
	GtkWidget *notebook;
	GtkWidget *vbox, *bbox, *button;
	
	is_myself = (! strcmp(info->uid, a->username));
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	updateinfo->window = window;
	
	updateinfo->old_info = qq_duplicate_info(info);

	g_signal_connect (G_OBJECT (window), "delete_event",
			    G_CALLBACK(info_window_deleteevent), NULL);
	g_signal_connect (G_OBJECT (window), "destroy",
			    G_CALLBACK(info_window_destroy), gc);
	gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
	
	if (is_myself)
		gtk_window_set_title (GTK_WINDOW (window), utf8("我的个人信息"));
	else
		gtk_window_set_title (GTK_WINDOW (window), utf8("查看好友信息"));
	gtk_container_set_border_width (GTK_CONTAINER (window), 5);

  vbox = gtk_vbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(window), vbox);
  
	notebook= gtk_notebook_new();
	gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_LEFT);
	
	label = gtk_label_new (utf8("基本资料"));
	gtk_notebook_append_page(GTK_NOTEBOOK (notebook), 
		createpage1 (is_myself,info,gc,updateinfo), label);

	label = gtk_label_new (utf8("联系方法"));
	gtk_notebook_append_page(GTK_NOTEBOOK (notebook), 
		createpage2 (is_myself,info,gc,updateinfo), label);

	label = gtk_label_new (utf8("详细资料"));
	gtk_notebook_append_page(GTK_NOTEBOOK (notebook), 
		createpage3 (is_myself,info,gc,updateinfo), label);

	if (is_myself) {
		label = gtk_label_new (utf8("网络安全"));
		gtk_notebook_append_page(GTK_NOTEBOOK (notebook), 
			createpage5 (is_myself,info,gc,updateinfo), label);
	} else {
		label = gtk_label_new (utf8("关于..."));
		gtk_notebook_append_page (GTK_NOTEBOOK (notebook), 
			createpage4 (is_myself,info,gc,updateinfo), label);
	}
	
	gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
	
	bbox = gtk_hbutton_box_new();
	gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
	gtk_box_set_spacing(GTK_BOX(bbox), 10);

  button = gtk_button_new_with_label(utf8("修改"));
  g_object_set_data(G_OBJECT(button), "user_data", (gpointer)updateinfo);
  g_signal_connect (G_OBJECT (button), "clicked",
		G_CALLBACK(info_window_change), gc);
	gtk_container_add (GTK_CONTAINER (bbox), button);
	
	if (is_myself) gtk_widget_set_sensitive(button, TRUE);
	else gtk_widget_set_sensitive(button, FALSE);

	button = gtk_button_new_with_label(utf8("刷新"));
	updateinfo->refresh_button = button;
	g_object_set_data(G_OBJECT(button),"user_data", (gpointer)updateinfo);
	g_signal_connect (G_OBJECT (button), "clicked",
			    G_CALLBACK(info_window_refresh), gc);
	gtk_container_add (GTK_CONTAINER (bbox), button);

	button = gtk_button_new_with_label(utf8("关闭"));
	g_signal_connect (G_OBJECT (button), "clicked",
			    G_CALLBACK(info_window_close), updateinfo->window);
	gtk_container_add (GTK_CONTAINER (bbox), button);

  gtk_box_pack_start(GTK_BOX(vbox), bbox, TRUE, TRUE, 5);	

  no_edit(updateinfo->uid_entry); // uid entry cannot be changed
  if (! is_myself) {
    no_edit(updateinfo->nick);
    no_edit(updateinfo->country);
    no_edit(updateinfo->age);
    no_edit(updateinfo->gender);
    no_edit(updateinfo->province);
    no_edit(updateinfo->city);
    no_edit(updateinfo->email);
    no_edit(updateinfo->address);
    no_edit(updateinfo->zipcode);
    no_edit(updateinfo->tel);
    no_edit(updateinfo->name);
    no_edit(updateinfo->blood);
    no_edit(updateinfo->college);
    no_edit(updateinfo->occupation);
    no_edit(updateinfo->zodiac);
    no_edit(updateinfo->horoscope);
    no_edit(updateinfo->homepage);
    gtk_text_view_set_editable(GTK_TEXT_VIEW(updateinfo->intro), FALSE);
  }
	gtk_widget_show_all(window);
}

/*****************************************************************************/
void qq_refresh_contact_info_dialog
(struct contact_info *info, GaimConnection *gc,
 struct _updating_contact_info *updateinfo) {
	GaimAccount *a = gc->account;
	
	if (updateinfo->old_info) g_free(updateinfo->old_info);
  updateinfo->old_info = qq_duplicate_info(info);
  
	gboolean is_myself = !(strcmp(info->uid, a->username));
  gtk_widget_set_sensitive(updateinfo->refresh_button,TRUE);
	
	if (is_myself) {
    g_object_set_data(G_OBJECT(updateinfo->face),
		  "user_data",GINT_TO_POINTER((gint)atoi(info->face)));
    qq_set_auth_type_radio(updateinfo, info->auth_type);
		qq_set_open_contact_radio(updateinfo, info->is_open_contact);
	}
	
	qq_set_entry(updateinfo->uid_entry, info->uid);
	qq_set_entry(updateinfo->nick, info->nick);	
	qq_set_entry(updateinfo->country, info->country);	
	qq_set_entry(updateinfo->age, info->age);
	qq_set_entry(updateinfo->gender, info->gender);
	qq_set_entry(updateinfo->province, info->province);
	qq_set_entry(updateinfo->city, info->city);
	qq_set_entry(updateinfo->email, info->email);
	qq_set_entry(updateinfo->address, info->address);
	qq_set_entry(updateinfo->zipcode, info->zipcode);
	qq_set_entry(updateinfo->tel, info->tel);
	qq_set_entry(updateinfo->name, info->name);
	gtk_entry_set_text(GTK_ENTRY(updateinfo->zodiac), 
	  get_name_by_index_str((gchar **)zodiac_names, info->zodiac, 1, 12));
	gtk_entry_set_text(GTK_ENTRY(updateinfo->horoscope),
    get_name_by_index_str((gchar **)horoscope_names, info->horoscope, 1, 12));
	gtk_entry_set_text(GTK_ENTRY(updateinfo->blood), 
    get_name_by_index_str((gchar **)blood_types, info->blood, 1, 4));
	qq_set_entry(updateinfo->college, info->college);
	qq_set_entry(updateinfo->occupation, info->occupation);
	qq_set_entry(updateinfo->homepage, info->homepage);
	
	qq_set_image(updateinfo->face, (uint8)atoi(info->face));
	
	qq_set_text(updateinfo->intro, info->intro);
}
